package com.rykj.qxj.apiService.controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rykj.qxj.server.util.object.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.rykj.qxj.result.BaseResponse;
import com.rykj.qxj.util.SignUtil;

import lombok.extern.log4j.Log4j;

@Log4j
@RestController
@RequestMapping("/v1/sign")
public class SignController {
	
	@GetMapping("/getSign")
	public void getSign(HttpServletRequest request,HttpServletResponse response) {
		String dataBaseUrl = request.getRequestURI().replace("/weather-api", "");
		//ApiManage apiManage = apiManageService.selectApiManageAndApiversionIdByApiIdAndVersionStatusAndVerifyStatusAndDeleteStatus(dataBaseUrl);
		String method = request.getMethod();
		String appKey = request.getHeader("appKey");
		if(StringUtils.isEmpty(appKey)){
			appKey= request.getParameter("appKey");
		}
		String createSign="";
		if(method.equals("GET")) {
			Map<String, String[]> parameterMap = request.getParameterMap();
			Map<String, Object> pMap = new HashMap<String, Object>();
			Set<String> keys = parameterMap.keySet();
			for (String key : keys) {
				pMap.put(key, parameterMap.get(key)[0]);
			}
			 createSign = SignUtil.createSign(pMap, appKey);
		}
		 doGetResult(new BaseResponse("200","GET请求的sign获取成功",createSign),response);
	}
	@PostMapping("/getSign")
	public void getSignPost(HttpServletRequest request,HttpServletResponse response) {
		String appKey = request.getHeader("appKey");
		if(StringUtils.isEmpty(appKey)){
			appKey= request.getParameter("appKey");
		}
		Map<String, Object> resMap=null;
	    //业务库中的插入操作
		String requestBodyJsonStr="";
		try {
			requestBodyJsonStr = getRequestBodyStr(request);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//将requestBody中请求字符串转化为map
		 resMap = SignUtil.convertJsonToMap(requestBodyJsonStr);
		 String createSign = SignUtil.createSign(resMap, appKey);
		 doGetResult(new BaseResponse("200","POST请求的sign获取成功",createSign),response);
		
		
	}
	@PutMapping("/getSign")
	public void getSignPut(HttpServletRequest request,HttpServletResponse response) {
		String appKey = request.getHeader("appKey");
		if(StringUtils.isEmpty(appKey)){
			appKey=  request.getParameter("appKey");
		}
		Map<String, Object> resMap=null;
	    //业务库中的插入操作
		String requestBodyJsonStr="";
		try {
			requestBodyJsonStr = getRequestBodyStr(request);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//将requestBody中请求字符串转化为map
		 resMap = SignUtil.convertJsonToMap(requestBodyJsonStr);
		 String createSign = SignUtil.createSign(resMap, appKey);
		 doGetResult(new BaseResponse("200","PUT请求的sign获取成功",createSign),response);
	}
	@DeleteMapping("/getSign")
	public void getSignDelete(HttpServletRequest request,HttpServletResponse response) {
		String appKey = request.getHeader("appKey");
		if(StringUtils.isEmpty(appKey)){
			appKey= request.getParameter("appKey");
		}
		Map<String, Object> resMap=null;
	    //业务库中的插入操作
		String requestBodyJsonStr="";
		try {
			requestBodyJsonStr = getRequestBodyStr(request);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//将requestBody中请求字符串转化为map
		 resMap = SignUtil.convertJsonToMap(requestBodyJsonStr);
		 String createSign = SignUtil.createSign(resMap, appKey);
		 doGetResult(new BaseResponse("200","DELETE请求的sign获取成功",createSign),response);
	}
	
	public  String getRequestBodyStr(ServletRequest request) throws IOException {
		BufferedReader reader = request.getReader();
		String str;
		String wholeStr="";
		while((str=reader.readLine())!=null) {
			wholeStr+=str;
		}
		return wholeStr;
	}
	
	private void doGetResult(BaseResponse resultResponse,ServletResponse response) {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		ObjectMapper mapper=new ObjectMapper();
		String resultJson="";
		try {
			resultJson = mapper.writeValueAsString(resultResponse);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		PrintWriter printWriter;
		try {
			printWriter = response.getWriter();
			printWriter.write(resultJson);
			printWriter.flush();
			printWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}

}
